/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class AtencionDAOImpl implements orm.dao.AtencionDAO {
	public Atencion loadAtencionByORMID(int idAtencion) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return loadAtencionByORMID(session, idAtencion);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion getAtencionByORMID(int idAtencion) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return getAtencionByORMID(session, idAtencion);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByORMID(int idAtencion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return loadAtencionByORMID(session, idAtencion, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion getAtencionByORMID(int idAtencion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return getAtencionByORMID(session, idAtencion, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByORMID(PersistentSession session, int idAtencion) throws PersistentException {
		try {
			return (Atencion) session.load(orm.Atencion.class, new Integer(idAtencion));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion getAtencionByORMID(PersistentSession session, int idAtencion) throws PersistentException {
		try {
			return (Atencion) session.get(orm.Atencion.class, new Integer(idAtencion));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByORMID(PersistentSession session, int idAtencion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Atencion) session.load(orm.Atencion.class, new Integer(idAtencion), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion getAtencionByORMID(PersistentSession session, int idAtencion, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Atencion) session.get(orm.Atencion.class, new Integer(idAtencion), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion[] listAtencionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return listAtencionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion[] listAtencionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return listAtencionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion[] listAtencionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Atencion as Atencion");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Atencion[]) list.toArray(new Atencion[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion[] listAtencionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Atencion as Atencion");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Atencion[]) list.toArray(new Atencion[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return loadAtencionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return loadAtencionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Atencion[] atencions = listAtencionByQuery(session, condition, orderBy);
		if (atencions != null && atencions.length > 0)
			return atencions[0];
		else
			return null;
	}
	
	public Atencion loadAtencionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Atencion[] atencions = listAtencionByQuery(session, condition, orderBy, lockMode);
		if (atencions != null && atencions.length > 0)
			return atencions[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateAtencionByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return iterateAtencionByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateAtencionByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Prueba1PDSPersistentManager.instance().getSession();
			return iterateAtencionByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateAtencionByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Atencion as Atencion");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateAtencionByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Atencion as Atencion");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion createAtencion() {
		return new orm.Atencion();
	}
	
	public boolean save(orm.Atencion atencion) throws PersistentException {
		try {
			orm.Prueba1PDSPersistentManager.instance().saveObject(atencion);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Atencion atencion) throws PersistentException {
		try {
			orm.Prueba1PDSPersistentManager.instance().deleteObject(atencion);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Atencion atencion)throws PersistentException {
		try {
			if(atencion.getPacienteidPaciente() != null) {
				atencion.getPacienteidPaciente().atencion.remove(atencion);
			}
			
			if(atencion.getPrestacionidPrestacion() != null) {
				atencion.getPrestacionidPrestacion().setAtencion(null);
			}
			
			return delete(atencion);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Atencion atencion, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(atencion.getPacienteidPaciente() != null) {
				atencion.getPacienteidPaciente().atencion.remove(atencion);
			}
			
			if(atencion.getPrestacionidPrestacion() != null) {
				atencion.getPrestacionidPrestacion().setAtencion(null);
			}
			
			try {
				session.delete(atencion);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Atencion atencion) throws PersistentException {
		try {
			orm.Prueba1PDSPersistentManager.instance().getSession().refresh(atencion);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Atencion atencion) throws PersistentException {
		try {
			orm.Prueba1PDSPersistentManager.instance().getSession().evict(atencion);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Atencion loadAtencionByCriteria(AtencionCriteria atencionCriteria) {
		Atencion[] atencions = listAtencionByCriteria(atencionCriteria);
		if(atencions == null || atencions.length == 0) {
			return null;
		}
		return atencions[0];
	}
	
	public Atencion[] listAtencionByCriteria(AtencionCriteria atencionCriteria) {
		return atencionCriteria.listAtencion();
	}
}
